package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/student")
public class UpdateStudentInformationController extends BaseController {

	@Autowired
	private BizService bizService;

	@Autowired
	private BizDao bizDao;
	
	private static Logger logger = LoggerFactory.getLogger(UpdateStudentInformationController.class);

	/**
	 * 修改学员基本信息
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updatePersonalInformation", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String updatePersonalInformation(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");
		
		try {   
			// 用户id
			String userId = request.getParameter("userId");
			// 电话号码
			String mobilePhone = request.getParameter("mobilePhone");
			//性别
			String sex = request.getParameter("sex");
			//地址
			String adress = request.getParameter("adress");
			//学车类型
			String driverTypeName = request.getParameter("driverTypeName");
			//是否编码
			String ecode = request.getParameter("ecode");
			String name = "";
			if(ecode!= null && !ecode.isEmpty() && ecode.equals("0")){
				//姓名
				name = URLDecoder.decode(request.getParameter("name"),"utf-8");
			}else{
				//姓名
				name = request.getParameter("name");
			}
			if(null != userId && !userId.equals("") 
					&& null != mobilePhone && !mobilePhone.equals("")){
				Map<String, Object> paramsMap = new HashMap<String, Object>();
				paramsMap.put("id", Integer.parseInt(userId));
				paramsMap.put("mobilePhone", mobilePhone);
				paramsMap.put("name", name);
				paramsMap.put("sex", sex);
				paramsMap.put("adress", adress);
				paramsMap.put("driverTypeName", driverTypeName);
				response = bizService.updateInfo(response,paramsMap);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}	
		} catch (Exception e) {
		    logger.info("updatePersonalInformation:"+ e.toString());
		    response.setErrorcode(1);
			response.setMessage("个人信息修改失败！");
		} finally {
			return response.toJSON();
		}

	}	
	
	
	/**
	 * 修改学员头像
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/UpdatePhoto", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String UpdatePhoto(HttpServletRequest request)
			throws UnsupportedEncodingException {
		
		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 用户Id
			String userId = request.getParameter("userId");
			//照片64进制数据
			String image64Base = request.getParameter("image64Base");
			
			if(userId != null && userId != ""
					&& image64Base != null && image64Base != ""){ 
				response = bizService.updateUserPhoto(userId,image64Base,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("用户头像为空！");
			}
		} catch (Exception e) {
			logger.info("UpdatePhoto:"+ e.toString());
			response.setReturnMsg(1,"修改失败！");
		} finally {
			return response.toJSON();
		}

	}
	

	
	
	/**
	 * 修改学员基础信息表的gid
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateStuGid", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateStuGid(HttpServletRequest request)
			throws UnsupportedEncodingException {
		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");
		// 影响行数
		int i = 0;
		try {
			// 第三方平台Id
			String gid = request.getParameter("gid");
			//学员基础信息表Id
			String baseId = request.getParameter("baseId");
			if(null!=gid&&!gid.isEmpty()&&null!=baseId&&!baseId.isEmpty()){
				Map<String, Object> paramsMap = new HashMap<String, Object>();
				paramsMap.put("baseId", Integer.parseInt(baseId));
				paramsMap.put("gid", gid);
				// 修改学员信息
				i = bizDao.updateStuGid(paramsMap);
				if (i > 0) {
					response.setErrorcode(0);
					response.setMessage("用户gid修改成功！");
				} else {
					response.setErrorcode(1);
					response.setMessage("用户gid修改失败！");
				}
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
			

		} catch (Exception e) {
			 logger.info("updateStuGid:"+ e.toString());
			 response.setErrorcode(1);
			 response.setMessage("用户gid修改异常！");
		} finally {
			return response.toJSON();
		}

	}

}
